package org.huawei260.TiKu;

import java.util.Scanner;
public class Suanfa {


//	public class TextStatistics {

		public static void main(String[] args) {
			Scanner scanner = new Scanner(System.in);
			System.out.println("Please enter the text (type 'exit' to finish):");

			StringBuilder textBuilder = new StringBuilder();
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				if ("exit".equals(line)) {
					break;
				}
				textBuilder.append(line).append("\n");
			}
			scanner.close();

			String text = textBuilder.toString();
			int count = countTexts(text);
			System.out.println("Total number of texts: " + count);
		}

		public static int countTexts(String text) {
			text = removeEscapedQuotes(text);
			text = removeStrings(text);
			text = removeComments(text);
			text = removeWhitespaces(text);
			text = handleConsecutiveSemicolons(text);
			text = handleLeadingAndTrailingSemicolon(text);
			return countSemiColons(text);
		}

		private static String removeEscapedQuotes(String text) {
			return text.replaceAll("\\\\.", "");
		}

		private static String removeStrings(String text) {
			// Remove double quoted strings
			text = text.replaceAll("\"(.*?)\"", "");
			// Remove single quoted strings
			text = text.replaceAll("\'(.*?)\'", "");
			return text;
		}

		private static String removeComments(String text) {
			// Remove comments starting with '-'
			text = text.replaceAll("-.*", "");
			return text;
		}

		private static String removeWhitespaces(String text) {
			return text.replaceAll("\\s+", "");
		}

		private static String handleConsecutiveSemicolons(String text) {
			return text.replaceAll(";+", ";");
		}

		private static String handleLeadingAndTrailingSemicolon(String text) {
			// Remove leading semicolon
			text = text.replaceAll(";\\s*", "");
			// Remove trailing semicolon if it's the last character
			if (text.endsWith(";")) {
				text = text.substring(0, text.length() - 1);
			}
			return text;
		}

		private static int countSemiColons(String text) {
			if (text.isEmpty()) {
				return 0;
			}
			String[] parts = text.split(";");
			int count = 0;
			for (String part : parts) {
				if (!part.trim().isEmpty()) {
					count++;
				}
			}
			return count;
		}
	}